package com.meringer.beans.ejb;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import com.meringer.entities.CarModel;
import com.meringer.entities.User;

/**
 * Session Bean implementation class UserAdminEJB
 */
@Stateless
@LocalBean
public class UserAdminEJB {
	
	@PersistenceContext
	private EntityManager em;

    /**
     * Default constructor. 
     */
    public UserAdminEJB() {
        // TODO Auto-generated constructor stub
    }

    public List<User> getUsers() {
		TypedQuery<User> queryUsers = em.createNamedQuery("User.getUsers", User.class);
		List<User> listUsers = queryUsers.getResultList();
		System.out.println(listUsers);
		return listUsers;
	}

	public void removeUser(Integer userId) {
		CarModel carModel = em.find(CarModel.class, userId);
		em.remove(carModel);
		System.out.println("removed user with ID " + userId);
	}
	
	public User getUserById(Integer userId) {
		return em.find(User.class, userId);
	}
	
	public void updateUser(User newUser) {
		System.out.println("update user in ejb");
		em.merge(newUser);
	}

	public void removeUser(User user) {
		user.setIsActive(false);
	}
}
